<?php

namespace App\Http\Controllers\Admin;

use App\Models\Course;
use Illuminate\Http\JsonResponse;

class CourseController extends AdminBaseController
{
    /**
     * 获取学科options
     * @return JsonResponse|object
     */
    public function getOptions() {
        $courses = Course::selectRaw('id as value, courseName as label, studyName, studyType')
            ->orderByRaw('studyType asc, id asc')
            ->get()
            ->groupBy('studyName');

        $list = collect($courses)->map(function ($group, $groupName) {
            return [
                'label' => $groupName,
                'value' => $group[0]->studyType,
                'children' => $group->map(function ($course) {
                    return [
                        'label' => $course->label,
                        'value' => $course->value
                    ];
                })->toArray()
            ];
        })->values()->toArray();

        return $this->restful($list);
    }
}
